package cn.bulibuqi.myblog.dao;

import cn.bulibuqi.myblog.bean.Blog;
import cn.bulibuqi.myblog.bean.BlogTag;
import cn.bulibuqi.myblog.bean.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-12-18:09
 */
@Repository
public class BlogDao {

    @Autowired
    BlogTagMapper blogTagMapper;
    @Autowired
    MyBlogMapper myBlogMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    TypeMapper typeMapper;

    @Transactional
    public void saveBlog(Blog blog) {



        //保存type  insert会 写回主键
       typeMapper.insert(blog.getType());
        //        存储blog
        //用写回的id给 id赋值
        blog.setTypeId(blog.getType().getId());
        myBlogMapper.insert(blog);
        //存储Tags
        List<Tag> tags = blog.getTags();

        if (tags!=null && tags.size()!=0){
            //存储tags
            for (Tag tag : tags) {
                tagMapper.insert(tag);
                BlogTag blogTag = new BlogTag();

                blogTag.setBlogId(blog.getId());
                blogTag.setTagId(tag.getId());

                int insert = blogTagMapper.insert(blogTag);
                System.out.println(insert+"           成功了没有  0 没有成功");

            }


        }





    }


    @Transactional
    public void saveBlogPlus(Blog blog) {

        //应该放在service层，
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());

        //存 blog 表
        myBlogMapper.insert(blog);
        //存储Tags
        List<Long> tags = blog.stringToList(blog.getTagIds());

        if (tags!=null && tags.size()!=0){
//            存blog-tags
            for (Long tag : tags) {
                BlogTag blogTag = new BlogTag();
                blogTag.setBlogId(blog.getId());
                blogTag.setTagId(tag);

                int insert = blogTagMapper.insert(blogTag);
//                System.out.println(insert+"           成功了没有  0 没有成功");

            }


        }





    }



    @Transactional
    public void updateBlogPlus(Blog blog) {

        //应该放在service层 以后再说
        blog.setUpdateTime(new Date());

        //更新blog 表
        myBlogMapper.updateById(blog);
        //更新Tags
        List<Long> tags = blog.stringToList(blog.getTagIds());
        //先删除，再插入的方式，简单粗暴
        QueryWrapper<BlogTag> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",blog.getId());
        blogTagMapper.delete(wrapper);


        if (tags!=null && tags.size()!=0){
//            存blog-tags
            for (Long tag : tags) {
                BlogTag blogTag = new BlogTag();
                blogTag.setBlogId(blog.getId());
                blogTag.setTagId(tag);

                int insert = blogTagMapper.insert(blogTag);
//                System.out.println(insert+"           成功了没有  0 没有成功");

            }


        }





    }


    //根据是否有主键id判断插入还是删除
    public void saveAndUpdate(Blog blog){
        if (blog.getId()==null){
            //没有id 为插入
            saveBlogPlus(blog);

        }else{
            //有id为更新
            updateBlogPlus(blog);

        }


    }



    @Transactional
    public boolean deleteBlog(Long id)
    {


        Blog allBlogInfoById = myBlogMapper.getAllBlogInfoById(id);
        if(allBlogInfoById==null){
            return false;
        }

        System.out.println(allBlogInfoById);

        QueryWrapper<BlogTag> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",id);
        int delete = blogTagMapper.delete(wrapper);
        System.out.println("删除了:---------------"+delete);
        List<Tag> tags = allBlogInfoById.getTags();
        if (tags!=null && tags.size()!=0){

            for (Tag tag : tags) {
                if ((myBlogMapper.getTagCount(tag.getId())==null)){
                    tagMapper.deleteById(tag.getId());
                }
            }

        }

        myBlogMapper.deleteById(id);

        //删除type








        return false;
    }
    @Transactional
    public boolean deleteBlogPlus(Long id){

        //删除blog_tag表
        QueryWrapper<BlogTag> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",id);
        int delete = blogTagMapper.delete(wrapper);
        //删除blog
        int i = myBlogMapper.deleteById(id);

        //以后改一下
        return true;

    }



}
